package co.yixiang.yshop.module.order.api;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import co.yixiang.yshop.framework.common.enums.OrderInfoEnum;
import co.yixiang.yshop.framework.common.util.object.BeanUtils;
import co.yixiang.yshop.module.order.api.productorder.ProductOrderApi;
import co.yixiang.yshop.module.order.api.productorder.dto.OrderDTO;
import co.yixiang.yshop.module.order.api.productorder.dto.ProductOrderDTO;
import co.yixiang.yshop.module.order.api.productorder.dto.StoreAfterSalesDTO;
import co.yixiang.yshop.module.order.api.productorder.dto.StoreOrderDTO;
import co.yixiang.yshop.module.order.controller.app.order.vo.AppStoreOrderQueryVo;
import co.yixiang.yshop.module.order.convert.storeorder.StoreOrderConvert;
import co.yixiang.yshop.module.order.dal.dataobject.storeaftersales.StoreAfterSalesDO;
import co.yixiang.yshop.module.order.dal.dataobject.storeorder.StoreOrderDO;
import co.yixiang.yshop.module.order.dal.dataobject.storeordercartinfo.StoreOrderCartInfoDO;
import co.yixiang.yshop.module.order.dal.mysql.storeaftersales.StoreAfterSalesMapper;
import co.yixiang.yshop.module.order.dal.mysql.storeorder.StoreOrderMapper;
import co.yixiang.yshop.module.order.dal.mysql.storeordercartinfo.StoreOrderCartInfoMapper;
import co.yixiang.yshop.module.order.service.storeorder.AppStoreOrderService;
import co.yixiang.yshop.module.order.service.storeorder.AsyncStoreOrderService;
import co.yixiang.yshop.module.product.controller.app.cart.vo.AppStoreCartQueryVo;
import co.yixiang.yshop.module.product.controller.app.product.vo.AppStoreProductRespVo;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static co.yixiang.yshop.module.order.enums.ErrorCodeConstants.ORDER_PAY_FINISH;
import static co.yixiang.yshop.module.order.enums.ErrorCodeConstants.STORE_ORDER_NOT_EXISTS;


@Service
public class ProductOrderApiImpl implements ProductOrderApi {

    @Resource
    @Lazy
    private AppStoreOrderService appStoreOrderService;

    @Resource
    private AsyncStoreOrderService asyncStoreOrderService;

    @Resource
    private StoreOrderMapper storeOrderMapper;
    @Resource
    private StoreOrderCartInfoMapper storeOrderCartInfoMapper;
    @Resource
    private StoreAfterSalesMapper storeAfterSalesMapper;

    @Override
    public ProductOrderDTO getPayParam(String orderId) {
//        Long userId = getLoginUserId();
        AppStoreOrderQueryVo orderInfo = appStoreOrderService.getOrderInfo(orderId, null);
        if (ObjectUtil.isNull(orderInfo)) {
            throw exception(STORE_ORDER_NOT_EXISTS);
        }
        if (orderInfo.getPaid().equals(OrderInfoEnum.PAY_STATUS_HAVE_PAID.getValue())) {
            throw exception(ORDER_PAY_FINISH);
        }

        ProductOrderDTO productOrderDTO = new ProductOrderDTO();
        productOrderDTO.setAmount(orderInfo.getPayPrice());
        return productOrderDTO;
    }

    @Override
    public void handleOrder(String orderId, Map<String, String> packageParams) {
        Long userId = getLoginUserId();
        AppStoreOrderQueryVo orderInfo = appStoreOrderService.getOrderInfo(orderId, null);
        appStoreOrderService.updatePayInfo(orderInfo, packageParams);
        // 异步统计
        asyncStoreOrderService.orderData(userId);
    }


    @Override
    public void handleWeChatPayNotify(String orderId, Map<String, String> resultMap) throws Exception {
        appStoreOrderService.wxPayNotify(orderId, resultMap);
    }

    @Override
    public void yuePay(String orderId, Long uid,Boolean helpPay) {
        appStoreOrderService.yuePay(orderId, uid,helpPay);
    }

    @Override
    public Long countOrderByProduct(Long productId) {
        return appStoreOrderService.countOrderByProduct(productId);
    }

    @Override
    public List<StoreOrderDTO> orderList(Long uid, int type) {
        List<AppStoreOrderQueryVo> appStoreOrderQueryVos = appStoreOrderService.orderList(uid, type, 1, 50);
        return StoreOrderConvert.INSTANCE.convertList03(appStoreOrderQueryVos);
    }

    @Override
    public OrderDTO getOrderInfo(String orderId) {
        StoreOrderDO storeOrderDO = storeOrderMapper.selectOne(new LambdaQueryWrapper<StoreOrderDO>()
                .eq(StoreOrderDO::getOrderId,orderId));
        return BeanUtils.toBean(storeOrderDO,OrderDTO.class);
    }

    @Override
    public String getOrderProductNames(String orderId) {
        List<StoreOrderCartInfoDO> cartInfos = storeOrderCartInfoMapper
                .selectList(new LambdaQueryWrapper<StoreOrderCartInfoDO>()
                .eq(StoreOrderCartInfoDO::getOrderId,orderId));
        List<String> stringList = cartInfos.stream()
                .map(cart -> {
                    AppStoreCartQueryVo cartQueryVo = JSON.parseObject(cart.getCartInfo(), AppStoreCartQueryVo.class);
                    AppStoreProductRespVo productRespVo = cartQueryVo.getProductInfo();

                    return productRespVo.getStoreName() + "(" + productRespVo.getAttrInfo().getSku() + ")";
                })
                .collect(Collectors.toList());
        return StrUtil.join(",",stringList);
    }

    @Override
    public StoreAfterSalesDTO getOrderAfterInfo(String orderId) {
        StoreAfterSalesDO storeAfterSalesDO = storeAfterSalesMapper.selectOne(new LambdaQueryWrapper<StoreAfterSalesDO>()
                .eq(StoreAfterSalesDO::getOrderCode,orderId));
        return BeanUtils.toBean(storeAfterSalesDO,StoreAfterSalesDTO.class);
    }
}
